package com.tmy.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmy.Dto.OrderDto;
import com.tmy.Dto.PageInfo;
import com.tmy.Dto.Result;
import com.tmy.entity.Hotel;
import com.tmy.entity.Order;
import com.tmy.entity.RoomInfo;
import com.tmy.entity.UserInfo;
import com.tmy.mapper.UserInfoMapper;
import com.tmy.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;


    /**
     * 用户注册
     */
    @Override
    public Result addUserInfo(UserInfo userInfo){
        Result result = new Result();
        try {
            userInfoMapper.insert(userInfo);
            result.setMsg("注册成功");
            result.setCode(200);
        }catch (Exception err){
            result.setCode(200);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 用户查询
     */
    @Override
    public Result queryUserInfo(UserInfo userInfo){
        Result result = new Result();
        try {
            LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
            if (userInfo.getUsername()!=null){
                lqw.eq(UserInfo::getUsername,userInfo.getUsername());
            }
            if (userInfo.getPassword()!=null){
                lqw.eq(UserInfo::getPassword,userInfo.getPassword());
            }
            if (userInfo.getEmail()!=null){
                lqw.eq(UserInfo::getEmail,userInfo.getEmail());
            }
            if (userInfo.getName()!=null){
                lqw.eq(UserInfo::getName,userInfo.getName());
            }
            if (userInfo.getPhone()!=null){
                lqw.eq(UserInfo::getPhone,userInfo.getPhone());
            }
            if (userInfo.getRole()!=null){
                lqw.eq(UserInfo::getRole,userInfo.getRole());
            }
            List<UserInfo> userInfoList =  userInfoMapper.selectList(lqw);
            result.setData(userInfoList);
            result.setMsg("注册成功");
            result.setCode(200);
        }catch (Exception err){
            result.setCode(200);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 用户分页查询
     */
    @Override
    public Result queryUserInfoPage(PageInfo pageInfo){
        Result result = new Result();
        try {
            UserInfo userInfo =JSON.parseObject(JSON.toJSONString(pageInfo.getParam()),UserInfo.class);
            LambdaQueryWrapper<UserInfo> lqw =new LambdaQueryWrapper<>();
            if (userInfo.getUsername()!=null){
                lqw.eq(UserInfo::getUsername,userInfo.getUsername());
            }
            if (userInfo.getPassword()!=null){
                lqw.eq(UserInfo::getPassword,userInfo.getPassword());
            }
            if (userInfo.getEmail()!=null){
                lqw.eq(UserInfo::getEmail,userInfo.getEmail());
            }
            if (userInfo.getName()!=null){
                lqw.eq(UserInfo::getName,userInfo.getName());
            }
            if (userInfo.getPhone()!=null){
                lqw.eq(UserInfo::getPhone,userInfo.getPhone());
            }
            if (userInfo.getRole()!=null){
                lqw.eq(UserInfo::getRole,userInfo.getRole());
            }
            Page<UserInfo> page = new Page<>(pageInfo.getPage(),pageInfo.getLimit());
            Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page, lqw);
            result.setData(userInfoPage);
            result.setCode(200);
            result.setMsg("订单查询成功");
        }catch (Exception err){
            result.setCode(200);
            result.setMsg(err.getCause());
        }
        return result;
    }
}
